import 'package:dartx/dartx.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:rxdart/rxdart.dart' as rx;
import 'package:sgy_flutter/page/detail/widget/ble_state_view.dart';

class BleStreamView<T> extends StatelessWidget {
  final BleManager? bleManager;
  final Widget Function(bool connected, T? data) builder;
  final Stream<T> stream;
  final T? initialData;
  final Widget? disconnectedWidget;

  const BleStreamView(
      {super.key,
      required this.bleManager,
      required this.builder,
      this.disconnectedWidget,
      this.initialData,
      required this.stream});

  @override
  Widget build(BuildContext context) {
    return BleStateView(
        bleManager: bleManager,
        builder: (connected) {
          return StreamBuilder<T>(
              initialData: initialData,
              stream: stream,
              builder: (c, s) => builder(connected, s.data));
        });
  }
}

class BleStreamView2<T> extends StatelessWidget {
  final BleManager? bleManager;
  final BleManager? rightBleManager;
  final Widget Function(bool leftConnected, bool rightConnected, T? data)
      builder;
  final Stream<T> stream;
  final T? initialData;
  final Widget? disconnectedWidget;

  const BleStreamView2(
      {super.key,
      required this.rightBleManager,
      required this.bleManager,
      required this.builder,
      this.disconnectedWidget,
      this.initialData,
      required this.stream});

  @override
  Widget build(BuildContext context) {
    return BleStateView2(
        bleManager: bleManager,
        rightBleManager: rightBleManager,
        builder: (leftConnected, rightConnected) {
          return StreamBuilder<T>(
              initialData: initialData,
              stream: stream,
              builder: (c, s) {
                if (s.hasError){
                  print("ble_stream_view error:${s.error}");
                }
                return builder(leftConnected, rightConnected, s.data);
              }
                  );
        });
  }
}
